<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        async function fn() {
            async function fetchs(bol) {
                console.log(bol, 6666)
                const res = await new Promise((resolve, reject) => {
                    setTimeout(() => {
                        bol !== undefined ? resolve(12) : reject(123)
                    }, 4000)
                }).catch((err) => {
                    console.log(err)
                })
                return res
            }
            const items = [...new Array(34)].map((v, i) => i)
            const fetch_arr = promiseFlats(items, 10)
            console.log(fetch_arr)
            // let index = 0;
            // for (const fetch of fetch_arr) {
            //     console.log(index++)
            //     await Promise.all(fetch.map(item => fetchs(item)))
            // }
            await Promise.all(fetch_arr.map(item => fetchEach(item)))
            async function fetchEach(arr) {
                // let index = 0;
                for (const i of arr) {
                    // console.log(index++);
                    await fetchs(i)
                }
                return
            }
        }
        fn()

        function promiseFlat(items, flat = 10) {
            // 二维数组
            const fetch_arr = []
            // 初始化存放的索引
            let arr_index = 0;
            // 多出来的值存放的索引
            let expand_index = 0;
            const length = items.length
            items.forEach((i, index) => {
                // 先找出每份多少个(如果总数小于10,则默认是1/份)
                const item_length = (Math.floor(length / flat) || 1)
                let arr = []
                // 判断当前索引值存不存在
                if (!fetch_arr[arr_index]) {
                    fetch_arr[arr_index] = []
                }
                arr = fetch_arr[arr_index]
                // 二维数组长度已经大于10,且当前数据的索引值+1 大于 10份的数量和  (处理10份的数量和多余的数量)
                if (fetch_arr.length >= flat && index + 1 > (item_length) * flat) {
                    fetch_arr[expand_index].push(i)
                    expand_index++
                } else {
                    arr.push(i)
                }

                if (arr.length >= item_length && fetch_arr.length < flat) {
                    arr_index++
                }

            })
            return fetch_arr
        }
        const p = (0,promiseFlat)()
        function promiseFlats(items, flat = 10) {
            // 二维数组
            const fetch_arr = [...Array.from({
                    length: flat,
                },
                (_, key) => ([]),
            )]
            let arr_index = 0;
            items.forEach((i, index) => {
                if (arr_index === flat) {
                    arr_index = 0
                }
                fetch_arr[arr_index].push(i)
                arr_index++
            })
            return fetch_arr
        }
    </script>
    <script>

    </script>
</head>

<body>

</body>

</html>